import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.List;

/**
 * redsi String 的基本操作
 */
public class RedisDemoString {
    public static void test1(Jedis jedis) {
        System.out.println("get 和 set");
        jedis.flushDB();

        jedis.set("k1","v1");
        jedis.set("k2","v2");
        jedis.set("k3","v3");

        String value = jedis.get("k1");
        System.out.println("value: " + value);

        value = jedis.get("k2");
        System.out.println("value: " + value);

        value = jedis.get("k100");
        System.out.println("value: " + value);
    }
    public static void test2(Jedis jedis) {
        System.out.println("mget 和 mset");
        jedis.flushDB();

        jedis.mset("k1","v1", "k2","v2","k3","v3");
        List<String> values = jedis.mget("k1","k2","k4", "k3");
        System.out.println("values: " + values);
    }

    public static void test3(Jedis jedis) {
        System.out.println("getrange 和 setrange");
        jedis.flushDB();

        jedis.set("key", "abcdefghijkl");
        String result = jedis.getrange("key",2,5);
        System.out.println("result: " + result);

        jedis.setrange("key", 2, "xxx");
        String update = jedis.get("key");
        System.out.println("update: " + update);
    }

    public static void test4(Jedis jedis) {
        System.out.println("append");
        jedis.flushDB();

        jedis.set("k1","abc");
        System.out.println("k1: " + jedis.get("k1"));
        jedis.append("k1", "def");
        System.out.println("k1: " + jedis.get("k1"));
    }
    public static void test5(Jedis jedis) {
        System.out.println("incr 和 decr");
        jedis.flushDB();

        jedis.set("key", "100");
        long incr = jedis.incr("key");
        System.out.println("incr: " + incr);
        System.out.println("value: " + jedis.get("key"));

        long decr = jedis.decr("key");
        System.out.println("decr: " + decr);
        System.out.println("value: " + jedis.get("key"));

        incr = jedis.incrBy("key", 50);
        System.out.println("incr: " + incr);
    }
    public static void main(String[] args) {
        JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:8888");
        try(Jedis jedis = jedisPool.getResource()) {
            // test1(jedis);
            // test2(jedis);
            // test3(jedis);
            // test4(jedis);
            test5(jedis);
        }
    }
}
